home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / oldwish / wishUtils.c < prev   
C/C++ Source or Header  |  1990-01-19  |  6KB  |  247 lines

  1. /* 
  2.  * wishUtils.c --
  3.  *
  4.  *    Useful stuff -- delivering error msgs, etc.
  5.  *
  6.  * Copyright 1987 Regents of the University of California
  7.  * All rights reserved.
  8.  * Permission to use, copy, modify, and distribute this
  9.  * software and its documentation for any purpose and without
  10.  * fee is hereby granted, provided that the above copyright
  11.  * notice appear in all copies.  The University of California
  12.  * makes no representations about the suitability of this
  13.  * software for any purpose.  It is provided "as is" without
  14.  * express or implied warranty.
  15.  */
  16.  
  17. #ifndef lint
  18. static char rcsid[] = "$Header: /a/newcmds/wish/RCS/wishUtils.c,v 1.4 89/01/11 11:58:50 mlgray Exp $ SPRITE (Berkeley)";
  19. #endif not lint
  20.  
  21.  
  22. #include "string.h"
  23. #include "sx.h"
  24. #include "util.h"
  25. #include "wishInt.h"
  26.  
  27. static    WishWindow    *menuWindow = NULL;
  28.  
  29.  
  30. /*
  31.  *----------------------------------------------------------------------
  32.  *
  33.  * WishCvtToPrintable --
  34.  *
  35.  *    Given a keystroke binding that may contain control characters
  36.  *    and/or meta characters, this routine produces a printable version
  37.  *    of the string.
  38.  *
  39.  * Results:
  40.  *    Up to length characters are stored at *result (including the
  41.  *    terminating NULL character).
  42.  *
  43.  * Side effects:
  44.  *    None.
  45.  *
  46.  *----------------------------------------------------------------------
  47.  */
  48. void
  49. WishCvtToPrintable(string, length, result)
  50.     char    *string;        /* Binding string to be converted. */
  51.     int        length;            /* No. of bytes available at result. */
  52.     char    *result;        /* Where to store printable form. */
  53. {
  54.     int        chunkSize;
  55.     char    chunk[20];
  56.     char    *p;
  57.  
  58.     /*
  59.      * Process the input string one character at a time to do the
  60.      * conversion.
  61.      */
  62.  
  63.     p = result;
  64.     for ( ; *string != 0; string++) {
  65.     int i;
  66.  
  67.     /*
  68.      * Figure out how to represent this particular character.
  69.      */
  70.  
  71.     i = *string & 0377;
  72.     if (i <= 040) {
  73.         if (i == 033) {
  74.         strcpy(chunk, "ESC");
  75.         } else if (i == '\n') {
  76.         strcpy(chunk, "RET");
  77.         } else if (i == '\t') {
  78.         strcpy(chunk, "TAB");
  79.         } else if (i == ' ') {
  80.         strcpy(chunk, "SPACE");
  81.         } else {
  82.         chunk[0] = 'C';
  83.         chunk[1] = '-';
  84.         chunk[2] = i - 1 + 'a';
  85.         chunk[3] = 0;
  86.         }
  87.     } else if (i < 0177) {
  88.         chunk[0] = i;
  89.         chunk[1] = 0;
  90.     } else if (i == 0177) {
  91.         strcpy(chunk, "DEL");
  92.     } else if ((i > 0240) && (i < 0377)) {
  93.         chunk[0] = 'M';
  94.         chunk[1] = '-';
  95.         chunk[2] = i & 0177;
  96.         chunk[3] = 0;
  97.     } else {
  98.         sprintf(chunk, "%#x", i);
  99.     }
  100.  
  101.     /*
  102.      * Add this chunk onto the result string (if it fits), with a
  103.      * preceding space if this isn't the first chunk.
  104.      */
  105.  
  106.     if (p != result) {
  107.         if (length < 1) {
  108.         break;
  109.         }
  110.         *p = ' ';
  111.         p++;
  112.         length--;
  113.     }
  114.     chunkSize = strlen(chunk);
  115.     if (length < chunkSize) {
  116.         strncpy(p, chunk, length);
  117.         p += length;
  118.         length = 0;
  119.         break;
  120.     } else {
  121.         strcpy(p, chunk);
  122.         p += chunkSize;
  123.         length -= chunkSize;
  124.     }
  125.     }
  126.  
  127.     if (length == 0) {
  128.     p--;
  129.     }
  130.     *p = 0;
  131. }
  132.  
  133.  
  134.  
  135. /*
  136.  *----------------------------------------------------------------------
  137.  *
  138.  * WishMenuProc --
  139.  *
  140.  *    This procedure is invoked whenver a menu command is invoked
  141.  *    in an wish window.  It now forces the interpreter to be the
  142.  *    one for the central display window.  Should I have a separate one
  143.  *    for the menus?
  144.  *
  145.  * Results:
  146.  *    None.
  147.  *
  148.  * Side effects:
  149.  *    The command.
  150.  *
  151.  *----------------------------------------------------------------------
  152.  */
  153. void
  154. WishMenuProc(command)
  155.     char    *command;        /* Command string */
  156. {
  157.     Window        w;
  158.     WishWindow    *aWindow;
  159.  
  160.     if (menuWindow == NULL) {
  161.     return;
  162.     }
  163.     w = menuWindow->surroundingWindow;
  164.     if (XFindContext(wishDisplay, w, wishWindowContext, (caddr_t) &aWindow)
  165.         != 0) {
  166.     Sx_Panic(wishDisplay, "Wish didn't recognize given window.");
  167.     }
  168.  
  169.     (void) WishDoCmd(aWindow, command);
  170. #ifdef NOTDEF
  171.     /* could have destroyed window stuff again... */
  172. #endif NOTDEF
  173. }
  174.  
  175.  
  176.  
  177. /*
  178.  *----------------------------------------------------------------------
  179.  *
  180.  * WishMakeMenu --
  181.  *
  182.  *    Duplicate the command strings in an array of menu entries, then
  183.  *    invoke Sx to create a menu.  It's needed in order to make sure
  184.  *    that all of the command strings in all menus, even the initial
  185.  *    default menus, are dynamically allocated.
  186.  *
  187.  * Results:
  188.  *    None.
  189.  *
  190.  * Side effects:
  191.  *    Memory gets allocated, and a menu gets created.
  192.  *
  193.  *----------------------------------------------------------------------
  194.  */
  195. void
  196. WishMakeMenu(aWindow, name, size, info)
  197.     register    WishWindow    *aWindow;/* Window in which to create menu. */
  198.     char    *name;                /* Name of menu. */
  199.     int        size;                /* Number of entries in menu. */
  200.     Sx_MenuEntry    info[];        /* Menu inforamtion. */
  201. {
  202.     Sx_MenuEntry    entries[SX_MAX_MENU_ENTRIES];
  203.     int            i;
  204.  
  205.     for (i = 0; i < size; i++) {
  206.     entries[i] = info[i];
  207.     entries[i].clientData = (ClientData) Util_Strcpy((char *) NULL,
  208.         (char *) info[i].clientData);
  209.     }
  210. fprintf(stderr, "windowFg is %d, windowBg is %d\n", aWindow->menuForeground, aWindow->menuBackground);
  211.     (void) Sx_MenuCreate(wishDisplay, aWindow->menuBar, name, size, entries,
  212.         aWindow->fontPtr, aWindow->menuForeground,
  213.         aWindow->menuBackground);
  214. }
  215.  
  216. /*
  217.  *----------------------------------------------------------------------
  218.  *
  219.  * WishMenuEntryProc --
  220.  *
  221.  *    This procedure is invoked by the Sx dispatcher whenever the
  222.  *    mouse enters a menu bar window.
  223.  *
  224.  * Results:
  225.  *    None.
  226.  *
  227.  * Side effects:
  228.  *    The variable menuWindow is updated to keep track of which
  229.  *    Wish window the cursor's in, so that we'll know when a menu
  230.  *    entry gets invoked.
  231.  *
  232.  *----------------------------------------------------------------------
  233.  */
  234. void
  235. WishMenuEntryProc(aWindow, eventPtr)
  236.     WishWindow *aWindow;        /* Window whose menu bar was just
  237.                      * entered. */
  238.     XEnterWindowEvent *eventPtr;    /* Event describing window entry. */
  239. {
  240. #ifdef NOTDEF
  241.     if (eventPtr->subwindow != NULL) {
  242.     return;
  243.     }
  244. #endif /* NOTDEF */
  245.     menuWindow = aWindow;
  246. }
  247.